/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateless.MRP.MaterialRequirement;

import entity.BillOfMaterial;
import entity.Ingredient;
import entity.MRP.MaterialRequirement.IndividualMatReq;
import entity.MRP.MaterialRequirement.WeeklyMaterialReq;
import entity.Product;
import java.util.ArrayList;
import java.util.List;
import javaclass.MaterialReqStt;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import session.stateless.ProductBeanLocal;

/**
 *
 * @author Vincent
 */
@Stateless
public class WeeklyMaterialReqBean implements WeeklyMaterialReqBeanLocal {

    @EJB
    BillOfMaterialBeanLocal billOfMaterialBeanLocal;
    
    @EJB
    ProductBeanLocal productBeanLocal;

    @EJB
    IngredientBeanLocal ingredientBeanLocal;
    
    @PersistenceContext (unitName = "M10-IS3102-ejbPU")
    private EntityManager entityManager; 
    
    @Override
    public Long createWeeklyMaterialReq(){
        //fake input
        //=========================================================================================
        List<Product> products = productBeanLocal.getProductList();
        long quantity1 = 100;
        long quantity2 = 200;
        long quantity3 = 150;
        long quantity4 = 250;
        
        long kgBatch1 = Math.round(Math.ceil(quantity1 * products.get(0).getKgPerCase() / 200));
        System.out.println(kgBatch1 + " " + products.get(0).getKgPerCase());
        long kgBatch2 = Math.round(Math.ceil(quantity2 * products.get(1).getKgPerCase() / 200));
        System.out.println(kgBatch2 + " " + products.get(1).getKgPerCase());
        long kgBatch3 = Math.round(Math.ceil(quantity3 * products.get(2).getKgPerCase() / 200));
        System.out.println(kgBatch3 + " " + products.get(2).getKgPerCase());
        long kgBatch4 = Math.round(Math.ceil(quantity4 * products.get(3).getKgPerCase() / 200));
        System.out.println(kgBatch4 + " " + products.get(3).getKgPerCase());
        //=========================================================================================
        
        WeeklyMaterialReq weeklyMaterialReq = new WeeklyMaterialReq();
        weeklyMaterialReq.setStatus(MaterialReqStt.PENDING);
        List<IndividualMatReq> individualMatReqs = new ArrayList<IndividualMatReq>();
        List<Ingredient> listOfIngredients = ingredientBeanLocal.getListOfIngredients();
        double quantity = 0.0;
        for (Ingredient i:listOfIngredients){
            System.out.println(products.get(0).getProductType()+" "+i.getName());
            BillOfMaterial billOfMaterial =  billOfMaterialBeanLocal.findBillOfMaterial(products.get(0).getProductType(), i.getName());
            if (billOfMaterial != null){
                quantity += kgBatch1 * billOfMaterial.getAmountRequired();
            }
            
            System.out.println(quantity);
            System.out.println(products.get(1).getProductType()+" "+i.getName());
            
            billOfMaterial =  billOfMaterialBeanLocal.findBillOfMaterial(products.get(1).getProductType(), i.getName());
            if (billOfMaterial != null){
                quantity += kgBatch2 * billOfMaterial.getAmountRequired();
            }
            
            System.out.println(quantity);
            System.out.println(products.get(2).getProductType()+" "+i.getName());
            
            billOfMaterial =  billOfMaterialBeanLocal.findBillOfMaterial(products.get(2).getProductType(), i.getName());
            if (billOfMaterial != null){
                quantity += kgBatch3 * billOfMaterial.getAmountRequired();
            }
            
            System.out.println(quantity);
            System.out.println(products.get(3).getProductType()+" "+i.getName());
            
            billOfMaterial =  billOfMaterialBeanLocal.findBillOfMaterial(products.get(3).getProductType(), i.getName());
            if (billOfMaterial != null){
                quantity += kgBatch4 * billOfMaterial.getAmountRequired();      
            }
            
            System.out.println(quantity);
            System.out.println(products.get(1).getProductType()+" "+i.getName());
            
            System.out.println("Success");
            IndividualMatReq individualMatReq = new IndividualMatReq();
            individualMatReq.setIngredient(i);
            individualMatReq.setTotalAmountRequired(quantity);
            entityManager.persist(individualMatReq);
            individualMatReqs.add(individualMatReq);
            quantity = 0.0;
        }
        weeklyMaterialReq.setIndividualMaterialRequirements(individualMatReqs);
        weeklyMaterialReq.setPlannedWeeklyDemand(null);
        entityManager.persist(weeklyMaterialReq);
        
        for(IndividualMatReq i:weeklyMaterialReq.getIndividualMaterialRequirements()){
            i.setWeeklyMaterialRequirement(weeklyMaterialReq);
        }
        entityManager.flush();
        return weeklyMaterialReq.getId();
    }
}
